package com.supermap.dataservice.agrinfo;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.agrinfo.dao.FarmingDao;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.agrinfo.FarmingService;
import com.supermap.dubbo.api.agrinfo.entity.Agrinfo;
import com.supermap.dubbo.api.agrinfo.entity.Farming;
import com.supermap.dubbo.api.agrinfo.entity.FarmingType;
import com.supermap.dubbo.api.common.CropService;
import com.supermap.dubbo.api.common.entity.Crop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:langhongshuai
 * @Description:作物业务逻辑层
 * @Date:18/1/8 上午11:20
 */
@Service
public class FarmingServiceImpl implements FarmingService {
    /**
     *使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(FarmingServiceImpl.class);

    @Resource
    private FarmingDao farmingDao;

    @Override
    public JSONUtil addFarming(Farming farming) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //保存农事信息
            farmingDao.saveFarming(farming);
            jsonUtil.setMsg("Add farming success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(farming);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("FarmingServiceImpl=>addFarming=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Add farming flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getAllFarming(Farming farming) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //获取农事信息
            List<Farming>  farmings = farmingDao.getFarming(farming);
            jsonUtil.setMsg("Get farming success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(farmings);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("FarmingServiceImpl=>getAllFarming=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get farming flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil updateFarming(Farming farming) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //修改农事信息
            farmingDao.updateFarming(farming);
            jsonUtil.setMsg("Update farming success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(null);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("FarmingServiceImpl=>updateFarming=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Update farming flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil deleteFarmingById(String farmingid) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //通过农事id删除农事信息
            farmingDao.deleteFarmingById(farmingid);
            jsonUtil.setMsg("Delete farming success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(null);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("FarmingServiceImpl=>deleteFarmingById=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Delete farming flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getAllFarmingType(int levels) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        List<FarmingType> farmingTypes =null;
        try {
            if(levels!=0){
                //获得所有的农事大类信息或者农事具体种类信息
                farmingTypes = farmingDao.getAllFarmingTypeByLevels(levels);
            }else{
                //获得所有的农事种类
                farmingTypes = farmingDao.getAllFarmingType();
                farmingTypes = organizeFarming(farmingTypes);
            }
            //获取所有的农事种类信息后返回的json数据
            jsonUtil.setMsg("Get all farming success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(farmingTypes);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("FarmingServiceImpl=>getAllFarming=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get all farming flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil saveFarmingType(FarmingType farmingType) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //保存作物
            farmingDao.saveFarmingType(farmingType);

            //获取所有的农事种类信息后返回的json数据
            jsonUtil.setMsg("Save farming success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(farmingType);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("FarmingServiceImpl=>saveFarming=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Save farming flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil updateFarmingType(FarmingType farmingType) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //更新农事种类的信息
            farmingDao.updateFarmingType(farmingType);
            jsonUtil.setMsg("update farming success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(farmingType);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("FarmingServiceImpl=>updateFarming=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("update farming flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil deleteFarmingType(String id) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //根据id删除农事种类信息
            farmingDao.deleteFarmingType(id);

            jsonUtil.setMsg("delte farming success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(id);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("FarmingServiceImpl=>deleteFarming=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("delete farming flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    /**
     * 给农事种类按等级划分
     * @param list
     * @return
     */
    private List<FarmingType> organizeFarming(List<FarmingType> list){
        List<FarmingType> firstList = new ArrayList<>();
        if(list.size()!=0){
            for (FarmingType a:list) {
                if (a.getLevel() == 1){
                    List<FarmingType> secondList = new ArrayList<>();
                for (FarmingType b : list) {
                    if (b.getLevel()==2){
                    //将农事大类放到第一级，具体农事放第二级
                    if(b.getParentid()==a.getId()) {
                        secondList.add(b);
                    }
                }

                }
                //大类添加子节点农事种类
                a.setChildNode(secondList);
                firstList.add(a);
            }
            }
        }
        return firstList;
    }
}
